<?php
    
	class ForumController{
		
		private $_forumView = null;
		private $_navigationView = null;
		private $_forumHandler = null;
		private $_currentUser = null;
		private $_isModerator = false;
		private $_output = null;
		private $_helpoutput = null;
				
		public function __construct($aForumView, $aForumHandler, $aNavigationView, $currentUser){
			$this->_forumView = $aForumView;
			$this->_navigationView = $aNavigationView;
			$this->_forumHandler = $aForumHandler;
			$this->_currentUser = $currentUser;
		}
		
		public function DoControll(){
			
			$this->_output = $this->_forumView->AddForumDiv();
			
			//Moderator
			if($this->_forumHandler->CheckIfUserIsModerator($this->_currentUser) == 1)
			{
				$this->DoModerator();
			}
			//Add Post
			if($this->_forumView->TriedToAddNewPost()){
				$this->AddPost();
			}
			//Delete Post
			else if($this->_navigationView->DidUserTryToDeletePost()){
				$this->DeletePost();
			}
			//Add Topic
			else if($this->_forumView->TriedToAddNewTopic()){
				$this->AddNewTopic();
			}
			//Edit Post
			else if($this->_forumView->TriedToEditPost()){
				$this->EditPost();
			}
			//Upload Userimage
			else if($this->_forumView->TriedToUploadImage()){
				$this->DoUpload();				
			}
			//Send PM
			else if($this->_forumView->TriedToSendPm()){
				$this->SendPrivateMessage();
			}
			
			//If the user is visiting the options page
			if($this->_navigationView->IsUserOptionsControllerActive() == TRUE)
			{
				$this->DoVisitUserOptions();
			}
			
			//If the user is adding a new Topic
			else if($this->_navigationView->IsNewTopicControllerActive() == TRUE){
				$this->DoVisitAddTopic();
			}
			
			//If the user is currently visiting a topic
			else if($this->_navigationView->GetActiveTopic() != FALSE){
				$this->DoVisitTopic();
			
			} 
			//If the user is currently visiting a category
			else if($this->_navigationView->GetActiveCategory() != FALSE){
				$this->DoVisitCategory();
			} 
			
			//If the user is currently visiting the main page
			else{
				$this->DoVisitMain();			
			}
			
			$this->_output .= $this->_navigationView->DrawUserOptionsLink();
			
			return $this->_output . $this->_helpoutput . $this->_forumView->AddEndDiv();
		}

		private function DoVisitUserOptions(){
			
			$this->_output .= $this->_forumView->AddUserPageDiv();
			$this->_output .= $this->_forumView->DrawUserHeader($this->_currentUser);
							
			//If the user is reading a PM
			if($this->_navigationView->GetActivePM() != FALSE){
				$activeMessageid = $this->_navigationView->GetActivePM();
				$activeMessage = $this->_forumHandler->GetPrivateMessageFromId($activeMessageid);
				
				$this->_output .= $this->_forumView->DrawPrivateMessageHeader();
				$this->_output .= $this->_forumView->DrawPrivateMessage($activeMessage, $this->_currentUser);
				
			}
			//If the user is writing a PM
			else if($this->_navigationView->IsUserTryingToWriteMessage())
			{
				$this->_output .= $this->_forumView->DrawPrivateMessageHeader();
				$this->_output .= $this->_forumView->DrawNewPmForm();
			}
			//If the user is visiting the userpage
			else{
				$this->_output .= $this->_forumView->DrawUserPageHeader();
				$this->_output .= $this->_forumView->DrawUserPage($this->_currentUser);
				
				if($this->_navigationView->IsSentActive() == True)
				{
					$messages = $this->_forumHandler->GetSentPrivateMessagesFromUser($this->_currentUser);
				}
				else {
					$messages = $this->_forumHandler->GetRecievedPrivateMessagesFromUser($this->_currentUser);
				}
				
				$this->_output .= $this->_forumView->DrawPrivateMessagesTopicsTop();
				$this->_output .= $this->_forumView->DrawPrivateMessagesTopics($messages);
				$this->_output .= $this->_navigationView->DrawReceivedButton() . $this->_navigationView->DrawSentButton();
				$this->_output .= $this->_navigationView->DrawWritePrivateMessageButton();
			}
			$this->_output .= $this->_forumView->AddEndDiv();
			$this->_output .= $this->_navigationView->DrawBackLink();
		}
		
		private function DoVisitAddTopic(){
			
			$categoryid = $this->_navigationView->GetActiveCategory();
			$category = $this->_forumHandler->GetCategoryFromId($categoryid);
			
			$this->_output .= $this->_forumView->DrawCategoryHeader($category);
			$this->_output .= $this->_forumView->DrawNewTopicHeader();
			$this->_output .= $this->_forumView->DrawNewTopicForm();
			$this->_output .= $this->_navigationView->DrawBackToCategoryLink($categoryid);
		}
		
		private function DoVisitCategory(){
			
			$categoryid = $this->_navigationView->GetActiveCategory();
			$topics = $this->_forumHandler->GetAllTopicsFromCategory($this->_navigationView->GetActiveCategory());
			$category = $this->_forumHandler->GetCategoryFromId($categoryid);
			
			$numberOfPages = (int)($this->_forumHandler->GetNumberOfTopicsFromCategory($categoryid) / 11) + 1;
			
			$page = $this->_navigationView->GetActivePage();
			
			$this->_output .= $this->_forumView->DrawCategoryHeader($category);
			$this->_output .= $this->_forumView->DrawTopicTop();
			$this->_output .= $this->_forumView->DrawTopics($topics, $page, $this->_isModerator);
			$this->_output .= $this->_navigationView->CreateNewTopicLink();
			$this->_output .= $this->_navigationView->DrawBackLink();
			$this->_output .= $this->_navigationView->DrawCategoryPageLinks($categoryid,$numberOfPages, $page);
		}
		
		private function DoVisitTopic(){
			
			$topicid = $this->_navigationView->GetActiveTopic();
			$posts = $this->_forumHandler->GetAllPostsFromTopic($this->_navigationView->GetActiveTopic());
			$topic = $this->_forumHandler->GetTopicFromId($topicid);
			$page = $this->_navigationView->GetActivePage();
			
			$numberOfPages = (int)(($this->_forumHandler->GetNumberOfRepliesFromTopic($topicid) + 1)/ 11) + 1;
			
			$this->_output .= $this->_forumView->DrawTopicHeader($topic);
			$this->_output .= $this->_forumView->DrawPostTop();
			$this->_output .= $this->_forumView->DrawPosts($posts, $this->_currentUser, $page, $this->_isModerator);
			
			//If the user is editing a Post
			if($this->_navigationView->DidUserTryToEditPost()){
				$postid = $this->_navigationView->GetActivePost();
				$message = $this->_forumHandler->GetPostMessageFromId($postid);
				
				$this->_output .= $this->_forumView->DrawEditPostHeader();
				$this->_output .= $this->_forumView->DrawEditPostForm($message);
			}
			//Otherwise draw the new post form
			else{
				$this->_output .= $this->_forumView->DrawNewPostHeader();
				$this->_output .= $this->_forumView->DrawNewPostForm();
			}
			$this->_output .= $this->_navigationView->DrawBackToCategoryLink($topic->_categoryid);
			$this->_output .= $this->_navigationView->DrawTopicPageLinks($topicid,$numberOfPages, $page);
		}
		
		private function DoVisitMain(){
			
			$categories = $this->_forumHandler->GetAllCategories();
			
			$this->_output .= $this->_forumView->DrawCategoryTop();
			$this->_output .= $this->_forumView->DrawCategories($categories);	
		}
		
		private function AddNewTopic(){
			
			$message = $this->_forumView->GetNewTopicMessage();
			$topicName = $this->_forumView->GetNewTopicName();
			
			if($this->_forumView->CheckTopicInfo($message,$topicName) != false)
			{
				$this->_helpoutput .= $this->_forumView->CheckTopicInfo($message,$topicName);
				$this->_navigationView->StayAtNewTopic();
			}
			else{
			$this->_forumHandler->AddNewTopicToCategory($topicName, $this->_currentUser, $this->_navigationView->GetActiveCategory());
			$topicid=$this->_forumHandler->GetTopicIdFromName($topicName);
			$this->_forumHandler->AddNewPostToTopic($topicid, $this->_currentUser, $message);
			$this->_navigationView->SetActiveTopic($topicid);
			}
		}
		
		private function SendPrivateMessage(){
			
			$receiver = $this->_forumView->GetNewPmReciever();
			$message = $this->_forumView->GetNewPmMessage();
			$topic = $this->_forumView->GetNewPmTopic();
			
			if($this->_forumView->CheckPrivateMessageInfo($receiver,$topic,$message) != false){
				$this->_helpoutput = $this->_forumView->CheckPrivateMessageInfo($receiver,$topic,$message);
				$this->_navigationView->StayAtWritePrivateMessage();
			}
			
			$this->_forumHandler->AddPrivateMessage($this->_currentUser,$receiver,$message,$topic);
		}
		
		private function DoModerator(){
			
			$this->_isModerator = True;
			$topicid = $this->_navigationView->GetTopicToEdit();
			
			if($this->_navigationView->DidUserTryToDeleteTopic()){
				$this->_forumHandler->RemoveTopic($topicid);
			}
			else if($this->_navigationView->DidUserTryToChangeSticky()){
				$this->_forumHandler->ChangeStickyForTopic($topicid);
			}
		}
		
		private function AddPost(){
			
			$message = $this->_forumView->GetNewPostMessage();
				
			if($this->_forumView->CheckMessageInfo($message) != false){
					$this->_helpoutput .= $this->_forumView->CheckMessageInfo($message);
			}
			
			$this->_forumHandler->AddNewPostToTopic($this->_navigationView->GetActiveTopic(), $this->_currentUser, $message);
		}
		
		private function EditPost(){
			
			$postid = $this->_navigationView->GetActivePost();
			
			if($this->_forumHandler->CheckIfUserMadePost($postid,$this->_currentUser) == TRUE || $this->_isModerator == TRUE){
				$message = $this->_forumView->GetEditPostMessage();
				
				if($this->_forumView->CheckMessageInfo($message) != false){
					$this->_helpoutput = $this->_forumView->CheckMessageInfo($message);
				}
				
				$this->_forumHandler->EditPost($postid,$message);
			}
		}
		
		private function DeletePost(){
			
			$postid = $this->_navigationView->GetActivePost();
			
			if($this->_forumHandler->CheckIfUserMadePost($postid,$this->_currentUser) == TRUE || $this->_isModerator == TRUE){
				$this->_forumHandler->RemovePost($postid);
			}
		}
		
		private function DoUpload(){
			
			$image = $this->_forumView->GetUploadedFile();
				
			if($this->_forumView->CheckImageInfo($image) != false){
				$this->_helpoutput = $this->_forumView->CheckImageInfo($image);
			}
			else{
				$this->_forumHandler->UploadUserImage($image, $this->_currentUser);
			}
		}
	}
